package com.corner.ui.fragment;

import android.content.Intent;
import android.net.Uri;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.corner.R;
import com.corner.app.ConsTants;
import com.corner.app.GlobalParams;
import com.corner.model.cornerservice.domain.Info;
import com.corner.presenter.imp.MinePresenter;
import com.corner.ui.activity.MainActivity;
import com.corner.ui.activity.ShowImageActivity;
import com.corner.ui.adapter.InfoAdapter;
import com.corner.ui.base.BaseFragment;
import com.corner.ui.view.IMineView;
import com.corner.utils.DateFormatUtil;
import com.corner.utils.NetUtil;
import com.corner.utils.TypeFaceUtil;
import com.corner.utils.view.FindView;
import com.corner.utils.view.widget.XListView;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.view.SimpleDraweeView;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class MineFragment extends BaseFragment<MinePresenter> implements View.OnClickListener, IMineView {

    @FindView(R.id.lv_profile_info)
    private XListView lvInfos;

    private LinearLayout header;
    private HeaderViewHolder headerViewHolder;

    private InfoAdapter infoAdapter;

    private Uri avaterUri;
    private View boottom;

    @Override
    protected void setOnclick() {
        lvInfos.addHeaderView(header);

        lvInfos.addFooterView(boottom);
        lvInfos.setAutoLoadEnable(true);
        headerViewHolder.ivProfileAvatar.setOnClickListener(this);
        lvInfos.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                if (!NetUtil.checkNet(activity)) {
                    lvInfos.stopRefresh();
                    return;
                }
                presenter.refresh();
            }

            @Override
            public void onLoadMore() {
                if (!NetUtil.checkNet(activity)) {
                    lvInfos.stopLoadMore();
                    return;
                }
                presenter.loadMore();
            }
        });
        infoAdapter = new InfoAdapter(new ArrayList<Info>(), activity, true);
        lvInfos.setAdapter(infoAdapter);
        presenter.refresh();
    }

    @Override
    protected MinePresenter createP() {
        return new MinePresenter();
    }


    @Override
    protected View initView(LayoutInflater inflater) {
        view = View.inflate(getActivity(), R.layout.frag_mine, null);
        initHeader(inflater);
        return view;
    }

    private void initHeader(LayoutInflater inflater) {
        if (header == null) {
            header = (LinearLayout) inflater.inflate(R.layout.profile_header, null);
            headerViewHolder = new HeaderViewHolder(header);
        } else {
            if (header.getParent() != null) {
                ((ListView) header.getParent()).removeHeaderView(header);
            }
        }

        if (boottom == null) {
            boottom = View.inflate(activity, R.layout.view_lv_bottom, null);
        } else {
            if (boottom.getParent() != null) {
                ((ListView) boottom.getParent()).removeHeaderView(boottom);
            }
        }
    }

    @Override
    public void onClick(View v) {
        if (v == headerViewHolder.ivProfileAvatar) {
            if (avaterUri != null) {
                Intent intent = new Intent(activity, ShowImageActivity.class);
                intent.putExtra(ShowImageActivity.IMAGE_URL, avaterUri.toString());
                startActivity(intent);
            }
        }
    }

    @Override
    public void showRefreshInfo(List<Info> infos) {
        if (infoAdapter == null) {
            infoAdapter = new InfoAdapter(infos, activity, true);
            lvInfos.setAdapter(infoAdapter);
        } else {
            infoAdapter.changeList(infos);
        }
    }

    @Override
    public void showLoadMoreInfo(List<Info> infos) {
        if (infoAdapter == null) {
            infoAdapter = new InfoAdapter(infos, activity, true);
            lvInfos.setAdapter(infoAdapter);
        } else {
            infoAdapter.changeList(infos);
        }
    }

    @Override
    public void refreshComplete() {
        lvInfos.stopRefresh();
        if (infoAdapter == null) {
            infoAdapter = new InfoAdapter(new ArrayList<Info>(), activity, true);
            lvInfos.setAdapter(infoAdapter);
        }
    }

    @Override
    public void loadMoreComplete() {
        lvInfos.stopLoadMore();
    }

    @Override
    public void setAvaterUri(Uri uri) {
        avaterUri = uri;
        headerViewHolder.ivProfileAvatar.setImageURI(avaterUri);
    }

    @Override
    public void setNickName(String nickName) {
        ((MainActivity) activity).setNickName(nickName);
    }

    @Override
    public void setCreateTime(Date date) {
        headerViewHolder.tvProfileTimeNotice.setText(DateFormatUtil.getTime("yyyy-MM-dd", date.getTime()) + getString(R.string.profile_header_create_time_back));
    }

    @Override
    public void setInfosCount(Integer infosCount, Integer picsCount) {
        String num = getString(R.string.profile_header_publish_num);
        num = StringUtils.replaceEach(num, new String[]{getString(R.string.profile_header_publish_num_num), getString(R.string.profile_header_pic_num_num)},
                new String[]{infosCount.toString(), picsCount.toString()});
        headerViewHolder.tvProfilePublishNotice.setText(Html.fromHtml(num));
    }

    @Override
    public void setLoadMoreEnable(boolean enable) {
        lvInfos.setPullLoadEnable(enable);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        infoAdapter.onDestroy();
    }

    public void upToRefresh() {
        lvInfos.setSelection(0);
        lvInfos.autoRefresh();
    }

    static class HeaderViewHolder {
        @InjectView(R.id.iv_profile_avatar)
        SimpleDraweeView ivProfileAvatar;
        @InjectView(R.id.tv_profile_publish_notice)
        TextView tvProfilePublishNotice;
        @InjectView(R.id.tv_profile_time_notice)
        TextView tvProfileTimeNotice;

        HeaderViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
